package com.dev.security.manager.dao;

import java.util.List;

import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.dev.security.config.dao.BaseRepository;
import com.dev.security.manager.beans.App;
import com.dev.security.manager.beans.RoleAppUnit;
import com.dev.security.manager.beans.RoleAppUnitId;

/**
 * @author 潜行的青衣
 * @see
 */
public interface RoleAppUnitRepository extends BaseRepository<RoleAppUnit, RoleAppUnitId> {

	@Query("select rau from RoleAppUnit rau where role_id=?1 order by rau.app.index")
	public List<RoleAppUnit> findAll(String role_id);
	
	@Query("select rau.app.id from RoleAppUnit rau where role_id=?1 order by rau.app.index")
	public List<String> findAllAppId(String role_id);
	
	@Query("select distinct rau.app from RoleAppUnit rau where role_id in ?1 order by rau.app.index")
	public List<App> findAllApps(List<String> role_ids);
	
	@Query("select count(rau) from RoleAppUnit rau where role_id in ?1 and rau.app.id=?2")
	public long countApps(List<String> role_ids, String app_id);
	
	@Modifying
	@Query("delete from RoleAppUnit where role_id=?1")
	public void delete(String role_id);
	
}
